package com.kevin.mybatis.configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.kevin.mybatis.configuration.datascope.handler.DataPermissionHandler;
import com.kevin.mybatis.configuration.datascope.handler.DataScopeHandler;
import com.kevin.mybatis.configuration.datascope.interceptor.DataScopeInterceptor;
import com.kevin.mybatis.configuration.datascope.interceptor.PageInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfiguration {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 数据范围插件
        interceptor.addInnerInterceptor(dataScopeInterceptor());
        // 防止入参错误导致全表更新或删除
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        // 分页插件
        interceptor.addInnerInterceptor(new PageInterceptor(DbType.H2));
        return interceptor;
    }

    @Bean
    public DataScopeInterceptor dataScopeInterceptor() {
        // 数据范围插件
        DataScopeInterceptor dataScopeInterceptor = new DataScopeInterceptor();
        dataScopeInterceptor.setDataScopeHandler(dataScopeHandler());
        return dataScopeInterceptor;
    }

    @Bean
    public DataScopeHandler dataScopeHandler() {
        // 数据范围处理器
        return new DataPermissionHandler();
    }


}
